/*
   This class can be used by objects such as dojo trees that expect a "getIconClass" method to return
   a css class name that represents an icon.

   This helper class expects the icons classes to have been made by the create_css_sprites.php script
   available on the "zend" side of this library.
   
   ---HOW TO USE---
   You can either:
      * (Recomended)       - Create this object in the custructor of the class you are working with and Use dojo._base.lang.hitch to "override" the icon class method
      * (Not Recomended)   - Inherit from this class if the class you are working with expects to find a getIconClass method

   ---EXAMPLE---

define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dijit/Tree',
      'noBusinessBase/helpers/IconClass'
   ],
   function(declare, Lang, Tree, IconClassHelper)
   {
      return declare([Tree],
      {
         constructor:function (args)
         {
            // Call Super
            dojo.safeMixin(this, args);

            this.mIconClassHelper = new IconClassHelper({});
            this.getIconClass = Lang.hitch(this.mIconClassHelper, this.mIconClassHelper.getIconClass);
         }
      });
   }
);

*/


define(
   [
      'dojo/_base/declare',
      'noBusinessBase/gui/ImagePathToCssClassConverter'
   ],
   function(declare, ImagePathToCssClassConverter)
   {
      return declare([],
      {
         altImageAttr:     '_altImage',      // Attribute name on object for alternate image
         altOpenImageAttr: '_altOpenImage',  // Attribute name on object for alternate open image
         imageAttr:        'Image',          // Attribute name on object for image
         openImageAttr:    'ImageOpened',    // Attribute name on object for opened image
         useAltImages:     true,             // Will try to use alternate image if available
         useOpenImage:     true,             // Will try to use alternate image if available and 'opened' is passed in as true
         constructor:function (args)
         {
            // Call Super
            dojo.safeMixin(this, args);
            this.mImagePathToCssClassConverter = new ImagePathToCssClassConverter({});
         },
         getIconClass:function (pItem, opened)
         {
            if (!pItem)
               return null;
      
            // Get the default image
            var image = pItem[this.imageAttr];
            if (!image)
               return null;

            // Should we use the 'opened' image?
            if(this.useOpenImage && opened && pItem[this.openImageAttr])
            {
               image = pItem[this.openImageAttr];
            }
      
            // Should we use the alternate image?
            if (pItem[this.altImageAttr] && this.useAltImages)
               image = pItem[this.altImageAttr];
            
            // Should we use the alternate 'opened' image?
            if(this.useOpenImage && this.useAltImages && opened && pItem[this.altOpenImageAttr])
            {
               image = pItem[this.altOpenImageAttr];
            }

            // Hack for malformed object stores
            if (typeof image == 'object')
               image = image[0];
      
            // Return the css class name for this image
            return this.mImagePathToCssClassConverter.getIconClass(image);
         }
      });
   }
);
